package demo3;

import java.util.Stack;

public class Test {
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String s = tokens[i];
            if (!isOperator(s)) {
                stack.push(Integer.parseInt(s));
            } else {
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(s) {
                    case "+" :
                        stack.push(num1 + num2);
                        break;
                    case "-" :
                        stack.push(num1 - num2);
                        break;
                    case "*" :
                        stack.push(num1 * num2);
                        break;
                    case "/" :
                        stack.push(num1 / num2);
                        break;
                }
            }
        }
        return stack.pop();
    }
    public static boolean isOperator(String s) {
        if (s.equals("+") || s.equals("-")  || s.equals("*") || s.equals("/")) {
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String[] strings = {"4","13","5","/","+"};
        System.out.println(evalRPN(strings));
    }
}

